﻿namespace SDFL.Helper
{
    using System;
    using System.Text;
    using System.Xml;

    public class XMLHelper
    {
        public static XmlDocument CreateXmlDocument()
        {
            return CreateXmlDocument(new Version(1, 0), Encoding.UTF8);
        }

        public static XmlDocument CreateXmlDocument(Version version, Encoding encoding)
        {
            XmlDocument document = new XmlDocument();
            document.AppendChild(document.CreateXmlDeclaration(version.ToString(), encoding.BodyName, string.Empty));
            return document;
        }

        public static XmlNode CreateXmlNode(XmlDocument xmlDocument, string name)
        {
            return CreateXmlNode(xmlDocument, name, string.Empty);
        }

        public static XmlNode CreateXmlNode(XmlDocument xmlDocument, string name, string value)
        {
            XmlNode node = xmlDocument.CreateNode(XmlNodeType.Element, name, string.Empty);
            node.AppendChild(xmlDocument.CreateTextNode(value));
            return node;
        }

        public static string GetAttributeValue(XmlNode xmlnd, string name)
        {
            return GetAttributeValue(xmlnd, name, string.Empty);
        }

        public static T GetAttributeValue<T>(XmlNode xmlnd, string name, T defaultT)
        {
            XmlAttribute attribute = xmlnd.Attributes[name];
            if (attribute == null)
            {
                return defaultT;
            }
            return ObjectConvertor.ToObject<T>(attribute.Value, defaultT);
        }

        public static string GetAttributeValue(XmlNode xmlnd, string name, string sErr)
        {
            XmlAttribute attribute = xmlnd.Attributes[name];
            if (attribute == null)
            {
                return sErr;
            }
            return attribute.Value;
        }

        public static T GetChildXmlNodeValue<T>(XmlNode xmlae, string name, T defaultT)
        {
            XmlElement element = xmlae[name];
            if (element == null)
            {
                return defaultT;
            }
            return GetXmlNodeValue<T>(element, defaultT);
        }

        public static string GetXmlNodeValue(XmlNode xmlae)
        {
            foreach (XmlNode node in xmlae.ChildNodes)
            {
                if (node.NodeType == XmlNodeType.Text)
                {
                    return node.Value;
                }
            }
            return string.Empty;
        }

        public static T GetXmlNodeValue<T>(XmlNode xmlae, T defaultT)
        {
            foreach (XmlNode node in xmlae.ChildNodes)
            {
                if (node.NodeType == XmlNodeType.Text)
                {
                    return ObjectConvertor.ToObject<T>(node.Value, defaultT);
                }
            }
            return defaultT;
        }

        public static void SetAttributeValue(XmlNode xmlne, string name, object value)
        {
            SetAttributeValue(xmlne, name, value, null);
        }

        public static void SetAttributeValue(XmlNode xmlne, string name, string value)
        {
            XmlAttribute node = xmlne.OwnerDocument.CreateAttribute(name);
            xmlne.Attributes.Append(node);
            node.Value = value;
        }

        public static void SetAttributeValue(XmlNode xmlne, string name, object value, string defaultStr)
        {
            string str = defaultStr;
            if (value != null)
            {
                str = value.ToString();
            }
            if (str != null)
            {
                SetAttributeValue(xmlne, name, str);
            }
        }

        public static void SetXmlNodeValue(XmlNode xmlae, object value)
        {
            SetXmlNodeValue(xmlae, value, string.Empty);
        }

        public static void SetXmlNodeValue(XmlNode xmlae, string value)
        {
            XmlDocument ownerDocument = xmlae.OwnerDocument;
            foreach (XmlNode node in xmlae.ChildNodes)
            {
                if (node.NodeType == XmlNodeType.Text)
                {
                    node.Value = value;
                }
            }
        }

        public static void SetXmlNodeValue(XmlNode xmlae, object value, string defaultStr)
        {
            string str = defaultStr;
            if (value != null)
            {
                str = value.ToString();
            }
            if (str != null)
            {
                SetXmlNodeValue(xmlae, str.ToString());
            }
        }
    }
}
